package com.mfangsoft.zhuangjialong.article.controller;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import com.mfangsoft.zhuangjialong.article.model.ArticleDetailModel;
import com.mfangsoft.zhuangjialong.article.model.ArticleEntity;
import com.mfangsoft.zhuangjialong.article.model.ArticleEvaluationModel;
import com.mfangsoft.zhuangjialong.article.service.ServerArticleService;
import com.mfangsoft.zhuangjialong.common.context.UserContext;
import com.mfangsoft.zhuangjialong.common.model.Page;
import com.mfangsoft.zhuangjialong.common.model.ResponseMessage;
import com.mfangsoft.zhuangjialong.common.utils.SysConstant;
import com.mfangsoft.zhuangjialong.integration.user.model.UserEntity;


/**
* @description：
* @author：Jianghua.Tang 
* @date：2016年7月5日
* 
*/
@Controller(value="ServerArticle")
@RequestMapping("/server")
public class ServerArticleController {
	
	@Autowired
	private ServerArticleService serverArticleServiceImpl;
	
	
	/**
	 * 查询文章列表
	 * @param param
	 * @return
	 */
	@RequestMapping(value = "/querynews", method = RequestMethod.POST)
	@ResponseBody
	@CrossOrigin
	public ResponseMessage<Page<ArticleDetailModel>> querynews(@RequestBody Page<ArticleDetailModel> param){
		ResponseMessage<Page<ArticleDetailModel>> message=new ResponseMessage<>();
		try {
			Page<ArticleDetailModel> queryNews = serverArticleServiceImpl.queryNews(param);
			message.setCode(SysConstant.SUCCESS_CODE);
			message.setMessage(SysConstant.SUCCESS_MSG);
			message.setResult(queryNews);
		} catch (Exception e) {
			e.printStackTrace();
			message.setCode(SysConstant.FAILURE_CODE);
			message.setMessage(SysConstant.FAILURE_MSG);
		}
		return message;
	}

	/**
	 * 新建文章
	 * @param articleEntity
	 * @return
	 */
	@RequestMapping(value = "/addArticle", method = RequestMethod.POST)
	@ResponseBody
	@CrossOrigin
	public ResponseMessage<String> addArticle(@RequestBody ArticleEntity articleEntity){
		ResponseMessage<String> message = new ResponseMessage<String>();
		if(serverArticleServiceImpl.addArticle(articleEntity)){
			message.setCode(SysConstant.SUCCESS_CODE);
			message.setMessage(SysConstant.SUCCESS_MSG);
		}else{
			message.setCode(SysConstant.FAILURE_CODE);
			message.setMessage(SysConstant.FAILURE_MSG);
		}
		return message;
	}
	
	/**
	 * 文章编辑
	 * @param articleEntity
	 * @return
	 */
	@RequestMapping(value = "/updateArticle", method = RequestMethod.POST)
	@ResponseBody
	@CrossOrigin
	public ResponseMessage<String> updateArticle(@RequestBody ArticleEntity articleEntity){
		ResponseMessage<String> message = new ResponseMessage<String>();
		if(serverArticleServiceImpl.updateArticle(articleEntity)){
			message.setCode(SysConstant.SUCCESS_CODE);
			message.setMessage(SysConstant.SUCCESS_MSG);
		}else{
			message.setCode(SysConstant.FAILURE_CODE);
			message.setMessage(SysConstant.FAILURE_MSG);
		}
		return message;
	}
	
	/**
	 * 文章删除
	 * @param 
	 * @return
	 */
	@RequestMapping(value = "/deleteArticleByArticleId/{articleId}")
	@ResponseBody
	@CrossOrigin
	public ResponseMessage<String> deleteArticleByArticleId(@PathVariable Long articleId){
		ResponseMessage<String> message = new ResponseMessage<String>();
		if(serverArticleServiceImpl.deleteArticleByArticleId(articleId)){
			message.setCode(SysConstant.SUCCESS_CODE);
			message.setMessage(SysConstant.SUCCESS_MSG);
		}else{
			message.setCode(SysConstant.FAILURE_CODE);
			message.setMessage(SysConstant.FAILURE_MSG);
		}
		return message;
	}
	
	
	/**
	 * 文章详情
	 * @param 
	 * @return
	 */
	@RequestMapping(value = "/queryarticledetails/{articleId}")
	@ResponseBody
	@CrossOrigin
	public ResponseMessage<ArticleEntity> queryarticledetails(@PathVariable Long articleId){
		ResponseMessage<ArticleEntity> message = new ResponseMessage<ArticleEntity>();
		try {
			ArticleEntity queryarticledetails = serverArticleServiceImpl.queryarticledetails(articleId);
			message.setCode(SysConstant.SUCCESS_CODE);
			message.setMessage(SysConstant.SUCCESS_MSG);
			message.setResult(queryarticledetails);
		} catch (Exception e) {
			e.printStackTrace();
			message.setCode(SysConstant.FAILURE_CODE);
			message.setMessage(SysConstant.FAILURE_MSG);
		}
		return message;
	}
	
	/**
	 * 查询文章评论列表
	 */
	@RequestMapping(value="/queryapparticleevaluation",method=RequestMethod.POST)
	@CrossOrigin
	@ResponseBody
	public ResponseMessage<Page<ArticleEvaluationModel>> queryAppArticleEvaluation(@RequestBody Page<ArticleEvaluationModel> param){
		ResponseMessage<Page<ArticleEvaluationModel>> message = new ResponseMessage<>();
		try {
			Page<ArticleEvaluationModel> queryAppArticleEvaluationForPage = serverArticleServiceImpl.queryAppArticleEvaluationForPage(param);
			message.setCode(SysConstant.SUCCESS_CODE);
			message.setMessage(SysConstant.SUCCESS_MSG);
			message.setResult(queryAppArticleEvaluationForPage);
		} catch (Exception e) {
			e.printStackTrace();
			message.setCode(SysConstant.FAILURE_CODE);
			message.setMessage(SysConstant.FAILURE_MSG);
		}
		return message;
	}
	
	/**
	 * 评价管理查询
	 * @param param
	 * @return
	 */
	@RequestMapping(value="/queryevaluationManagement",method=RequestMethod.POST)
	@CrossOrigin
	@ResponseBody
	public ResponseMessage<Page<ArticleEvaluationModel>> queryevaluationManagement(@RequestBody Page<ArticleEvaluationModel> param){
		ResponseMessage<Page<ArticleEvaluationModel>> message = new ResponseMessage<>();
		try {
			Page<ArticleEvaluationModel> queryevaluationManagement = serverArticleServiceImpl.queryevaluationManagement(param);
			message.setCode(SysConstant.SUCCESS_CODE);
			message.setMessage(SysConstant.SUCCESS_MSG);
			message.setResult(queryevaluationManagement);
		} catch (Exception e) {
			e.printStackTrace();
			message.setCode(SysConstant.FAILURE_CODE);
			message.setMessage(SysConstant.FAILURE_MSG);
		}
		return message;
	}
	
	/**
	 * 评论的违规屏蔽
	 */
	@RequestMapping(value = "/updateEvaluationStateByEvaluationId/{evaluationId}/{state}")
	@ResponseBody
	@CrossOrigin
	public ResponseMessage<String> updateEvaluationStateByEvaluationId(@PathVariable Long evaluationId,@PathVariable Long state){
		ResponseMessage<String> message = new ResponseMessage<String>();
		if(serverArticleServiceImpl.updateEvaluationStateByEvaluationId(evaluationId,state)){
			message.setCode(SysConstant.SUCCESS_CODE);
			message.setMessage(SysConstant.SUCCESS_MSG);
		}else{
			message.setCode(SysConstant.FAILURE_CODE);
			message.setMessage(SysConstant.FAILURE_MSG);
		}
		return message;
	}
}
